package com.example.communitypro.controller;


import com.example.communitypro.pojo.*;
import com.example.communitypro.service.HomeMessageService;
import com.example.communitypro.util.DateUtil;
import com.example.communitypro.util.SpringContextUtils;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
public class HomeController {

    @Autowired
    private HomeMessageService homeMessageService;


    @RequestMapping(value = "/toHomeNews")
    @ResponseBody
    @CrossOrigin
    public Queue<HomeMessage> addPost(ModelMap map){
        HomeMessageQueue homeMessageQueue = (HomeMessageQueue) SpringContextUtils.getBean("homeMessageQueue");
        /*map.put("datas", homeMessageQueue1);
        return map;*/
        return homeMessageQueue.getHomeMessageQueue();
    }

    @RequestMapping(value = "/getLogList")
    @ResponseBody
    @CrossOrigin
    public Result getLogList(){
        LoginLogList loginLogList = (LoginLogList) SpringContextUtils.getBean("loginLogList");
        Collections.reverse(loginLogList.getLoginLogList());
//        loginLogList.getLoginLogList().add(new LoginLog("11", "啦啦啦啦啦啦啦啦啦啦", DateUtil.getTimeByTransformation(new Date(), "yyyy-MM-dd HH:mm:ss")));
//        loginLogList.getLoginLogList().add(new LoginLog("22", "yyyyy还不错", DateUtil.getTimeByTransformation(new Date(), "yyyy-MM-dd HH:mm:ss")));
//        loginLogList.getLoginLogList().add(new LoginLog("33", "啦啦啦啦啦啦啦啦啦啦", DateUtil.getTimeByTransformation(new Date(), "yyyy-MM-dd HH:mm:ss")));
        return Result.ok().message("查找登陆信息成功").data("loginLogList",loginLogList.getLoginLogList()).count(loginLogList.getLoginLogList().size());
    }


    @RequestMapping(value = "/getLogListByDate")
    @ResponseBody
    @CrossOrigin
    public Result getLogListByDate(String startDate,String endDate){
        LoginLogList loginLogList = (LoginLogList) SpringContextUtils.getBean("loginLogList");

        List<LoginLog> loginLogList1 = loginLogList.getLoginLogList();
        List<LoginLog> resLoginLogList = new ArrayList<>();

        if ("".equals(startDate) && "".equals(endDate)){
            return Result.ok().message("查找登陆信息成功").data("loginLogList",loginLogList.getLoginLogList()).count(loginLogList.getLoginLogList().size());
        }else if ("".equals(startDate)){
            Date eDate = DateUtil.StrToDate(endDate);
            for (LoginLog loginLog : loginLogList1) {
                if (eDate.getTime() >= DateUtil.StrToDate(loginLog.getDate()).getTime()){
                    resLoginLogList.add(loginLog);
                }
            }
            return Result.ok().message("查找登陆信息成功").data("loginLogList",resLoginLogList).count(resLoginLogList.size());
        }else if("".equals(endDate)){

            Date sDate = DateUtil.StrToDate(startDate);
            for (LoginLog loginLog : loginLogList1) {
                if (sDate.getTime() <= DateUtil.StrToDate(loginLog.getDate()).getTime()){
                    resLoginLogList.add(loginLog);
                }
            }
            return Result.ok().message("查找登陆信息成功").data("loginLogList",resLoginLogList).count(resLoginLogList.size());
        }else {
            Date sDate = DateUtil.StrToDate(startDate);
            Date eDate = DateUtil.StrToDate(endDate);
            for (LoginLog loginLog : loginLogList1) {
                if (sDate.getTime() <= DateUtil.StrToDate(loginLog.getDate()).getTime() && eDate.getTime() >= DateUtil.StrToDate(loginLog.getDate()).getTime()){
                    resLoginLogList.add(loginLog);
                }
            }
            return Result.ok().message("查找登陆信息成功").data("loginLogList",resLoginLogList).count(resLoginLogList.size());
        }


    }



}